package io.github.libkodi.basic_gateway.tools;

import java.io.InputStream;

import io.vertx.core.Future;
import io.vertx.core.buffer.Buffer;

public class StreamUtils {

	public static Future<Buffer> read(InputStream os) {
		return Future.future(promise -> {
			if (os != null) {
				Buffer buff = Buffer.buffer();
				
				int len = 0;
				byte[] bytes = new byte[1024];
				
				try {
					while ((len = os.read(bytes)) > 0) {
						buff.appendBytes(bytes, 0, len);
					}
					
					promise.complete(buff);
				} catch (Exception e) {
					promise.fail(e);
				}
			} else {
				promise.fail("Invalid InputStream");
			}
		});
	}

}
